
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using Yozeev.SystemConfig;
using Yozeev.SystemConfig.Dtos;
using Yozeev.SystemConfig.DomainService;
using Yozeev.SystemConfig.Authorization;
using Yozeev.Common;

namespace Yozeev.SystemConfig
{
    /// <summary>
    /// BranchUser应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class BranchUserAppService : YozeevAppServiceBase, IBranchUserAppService
    {
        private readonly IRepository<BranchUser, Guid> _entityRepository;

        private readonly IBranchUserManager _entityManager;

        private readonly IQueryHelper _queryHelper;

        /// <summary>
        /// 构造函数 
        ///</summary>
        public BranchUserAppService(
        IRepository<BranchUser, Guid> entityRepository
        ,IBranchUserManager entityManager
			, IQueryHelper queryHelper
        )
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
            _queryHelper = queryHelper;
        }


        /// <summary>
        /// 获取BranchUser的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<BranchUserListDto>> GetPaged(GetBranchUsersInput input)
		{

            var expression = _queryHelper.WhereExp<BranchUser>(input.queryData);
            var query = from branchUser in _entityRepository.GetAll().AsNoTracking()
                        .Where(expression)
                        select new
                        {
                            branchUser,
                            branchUser.User,
                            branchUser.Branch
                        };
            var list = new List<BranchUser>();
            query.ToList().ForEach(temp => {
                var _branchUser = temp.branchUser;
                _branchUser.User = temp.User;
                _branchUser.Branch = temp.Branch;
                list.Add(_branchUser);
            });
            // TODO:根据传入的参数添加过滤条件


            var count = list.Count();
            var entityList = list;

            // var entityListDtos = ObjectMapper.Map<List<BranchUserListDto>>(entityList);
            var entityListDtos =entityList.MapTo<List<BranchUserListDto>>();

			return new PagedResultDto<BranchUserListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取BranchUserListDto信息
		/// </summary>
		public async Task<BranchUserListDto> GetById(EntityDto<Guid> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<BranchUserListDto>();
		}

		/// <summary>
		/// 获取编辑 BranchUser
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<GetBranchUserForEditOutput> GetForEdit(NullableIdDto<Guid> input)
		{
			var output = new GetBranchUserForEditOutput();
BranchUserEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<BranchUserEditDto>();

				//branchUserEditDto = ObjectMapper.Map<List<branchUserEditDto>>(entity);
			}
			else
			{
				editDto = new BranchUserEditDto();
			}

			output.BranchUser = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改BranchUser的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task CreateOrUpdate(CreateOrUpdateBranchUserInput input)
		{

			if (input.BranchUser.Id.HasValue)
			{
				await Update(input.BranchUser);
			}
			else
			{
				await Create(input.BranchUser);
			}
		}


		/// <summary>
		/// 新增BranchUser
		/// </summary>
		protected virtual async Task<BranchUserEditDto> Create(BranchUserEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <BranchUser>(input);
            var entity=input.MapTo<BranchUser>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<BranchUserEditDto>();
		}

		/// <summary>
		/// 编辑BranchUser
		/// </summary>
		protected virtual async Task Update(BranchUserEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除BranchUser信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task Delete(EntityDto<Guid> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除BranchUser的方法
		/// </summary>
		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		/// <summary>
		/// 导出BranchUser为excel表,等待开发。
		/// </summary>
		/// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

    }
}


